package com.egao.trade.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.trade.biz.TradeCartBiz;
import com.egao.trade.entity.TradeCart;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2022-04-18 16:06:31
 * @version ：1.0
 */
@Repository
public class TradeCartBizImpl extends BaseDao implements TradeCartBiz {

	@Override
	public JsonResult list(PageResult2<T> page, TradeCart tradeCart, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<TradeCart> rm = new BeanPropertyRowMapper<TradeCart>(TradeCart.class);
		StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.code");
    sql.append(",t.goods_id,t.price,t.freight,t.order_num");
		sql.append(",t.create_time,t.create_user,t.update_time,t.update_user");
    sql.append(",suser.nick_name createName ");
		sql.append(",t2.type1");
		sql.append(",t2.type2,t2.pic_url,t2.describes,t2.ref_price");
		sql.append(",t2.least_order,t2.payment,t2.goods_size,t2.limit_use");
		sql.append(",t2.color,t2.nums,t2.price price2,t2.freight freight2");
    sql.append(" from trade_cart t ");
    sql.append(" inner join trade_goods t2  on t2.id=t.goods_id ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");

    sql.append(" where t.deleted=0 ");
		sql.append(" and t.create_user=:create_user ");
		map.put("create_user", loginUser.getUserId() + "");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(tradeCart)){
//			if(Utils.isNotEmpty(tradeCart.getName())){
//				sql.append(" and instr(t.name,:name)>0 ");
//				map.put("name", tradeCart.getName());
//			}
      if(Utils.isNotEmpty(tradeCart.getSort())&&Utils.isNotEmpty(tradeCart.getOrder())){
        sql.append(" order by t."+tradeCart.getSort()+" "+tradeCart.getOrder());
      } else {
        sql.append(" order by t.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<TradeCart> list(TradeCart tradeCart, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<TradeCart> rm = new BeanPropertyRowMapper<TradeCart>(TradeCart.class);
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.code");
    sql.append(",t.goods_id,t.price,t.freight,t.order_num");
		sql.append(",t.create_time,t.create_user,t.update_time,t.update_user");
    sql.append(",suser.nick_name createName ");
		sql.append(",t2.type1");
		sql.append(",t2.type2,t2.pic_url,t2.describes,t2.ref_price");
		sql.append(",t2.least_order,t2.payment,t2.goods_size,t2.limit_use");
		sql.append(",t2.color,t2.nums,t2.price price2,t2.freight freight2");
		sql.append(" from trade_cart t ");
		sql.append(" inner join trade_goods t2  on t2.id=t.goods_id ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    
    sql.append(" where t.deleted=0 ");
		sql.append(" and t.create_user=:create_user ");
		map.put("create_user", loginUser.getUserId() + "");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(tradeCart)){
      if(Utils.isNotEmpty(tradeCart.getSort())&&Utils.isNotEmpty(tradeCart.getOrder())){
        sql.append(" order by t."+tradeCart.getSort()+" "+tradeCart.getOrder());
      } else {
        sql.append(" order by t.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public TradeCart getById(TradeCart tradeCart) {
		RowMapper<TradeCart> rm = new BeanPropertyRowMapper<TradeCart>(TradeCart.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.code");
    sql.append(",t.goods_id,t.price,t.freight,t.order_num");
		sql.append(",t.create_time,t.create_user,t.update_time,t.update_user");
    sql.append(",suser.nick_name createName ");
		sql.append(",t2.type1");
		sql.append(",t2.type2,t2.pic_url,t2.describes,t2.ref_price");
		sql.append(",t2.least_order,t2.payment,t2.goods_size,t2.limit_use");
		sql.append(",t2.color,t2.nums,t2.price price2,t2.freight freight2");
		sql.append(" from trade_cart t ");
		sql.append(" inner join trade_goods t2  on t2.id=t.goods_id ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    
    sql.append(" where t.deleted=0 and t.id=:id ");
		map.put("id", tradeCart.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(TradeCart tradeCart, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into trade_cart (");
    sql.append("organization_id,code,goods_id,price");
		sql.append(",freight,order_num,create_user,create_time) values ( ");
    sql.append(":organization_id,:code,:goods_id,:price");
		sql.append(",:freight,:order_num,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("code", "");
    map.put("goods_id", tradeCart.getGoodsId()==null?null:tradeCart.getGoodsId()+"");
    map.put("price", tradeCart.getPrice());
    map.put("freight", tradeCart.getFreight());
    map.put("order_num", "1");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(TradeCart tradeCart, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update trade_cart set ");
    sql.append("code=:code,goods_id=:goods_id,price=:price ");
		sql.append(",freight=:freight,order_num=:order_num,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
    map.put("code", "");
    map.put("goods_id", tradeCart.getGoodsId()==null?null:tradeCart.getGoodsId()+"");
    map.put("price", tradeCart.getPrice());
    map.put("freight", tradeCart.getFreight());
    map.put("order_num", tradeCart.getOrderNum()==null?null:tradeCart.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", tradeCart.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(TradeCart tradeCart, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(tradeCart.getId().length()>0){
			for(String ID : tradeCart.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update trade_cart set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
